package com.qzxy.premiumhotelsystem.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzxy.premiumhotelsystem.common.constant.RedisKeyConstant;
import com.qzxy.premiumhotelsystem.common.constant.VipStatusConstant;
import com.qzxy.premiumhotelsystem.common.result.Result;
import com.qzxy.premiumhotelsystem.domain.dto.VipDTO;
import com.qzxy.premiumhotelsystem.domain.dto.VipRenewDTO;
import com.qzxy.premiumhotelsystem.domain.entity.Guests;
import com.qzxy.premiumhotelsystem.domain.entity.Vip;
import com.qzxy.premiumhotelsystem.domain.vo.VipVO;
import com.qzxy.premiumhotelsystem.mapper.VipTypesMapper;
import com.qzxy.premiumhotelsystem.service.GuestsService;
import com.qzxy.premiumhotelsystem.service.VipService;
import com.qzxy.premiumhotelsystem.mapper.VipMapper;
import com.qzxy.premiumhotelsystem.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author 14912
* @description 针对表【vip】的数据库操作Service实现
* @createDate 2024-09-12 15:55:14
*/
@Service
@Slf4j
public class VipServiceImpl extends ServiceImpl<VipMapper, Vip>
    implements VipService{
    @Resource
    private RedisUtil redisUtil;

    @Resource
    private VipMapper vipMapper;

    @Resource
    private GuestsService guestsService;

    @Resource
    private VipTypesMapper vipTypesMapper;

    @Override
    public List<VipVO> getActiveVip() {

        //TODO 暂时假设从这取，到时候等用户模块完善后看是从这还是redis
        String telephone =(String) StpUtil.getLoginId();
        Guests guests = guestsService.getByTelephone(telephone);
        Integer guestId = guests.getGuestId();

        log.info("查询用户：{}已经激活的所有VIP会员信息",guestId);

//        String cacheKey = RedisKeyConstant.VIP_KEY + guestId;
//        List<VipVO> cacheList = redisUtil.getCacheList(cacheKey);
        //如果从redis查询到为空，那么从数据库查并缓存，便于下次查询
//        if (CollUtil.isEmpty(cacheList)){
            List<VipVO> list= vipMapper.getVipsAndType(guestId);
//            redisUtil.setCacheList(cacheKey,list);
            return list;
//        }
//        return cacheList;
    }

    @Override
    @Transactional
    public Result renewVip(VipRenewDTO vipRenewDTO) {
        //TODO 暂时假设从这取，到时候等用户模块完善后看是从这还是redis
        String telephone = (String) StpUtil.getLoginId();
        Guests guests = guestsService.getByTelephone(telephone);
        Integer guestId = guests.getGuestId();

        log.info("用户：{}开始续费的VIP会员：{}",guestId,vipRenewDTO.getVipTypeId());
        VipVO vipVO = vipMapper.selectVipVo(guestId,vipRenewDTO.getVipTypeId());
        if (vipVO == null || VipStatusConstant.VIP_EXPIRED.equals(vipVO.getStatus())) {
            return Result.build(null,500,"会员已过期或不存在");
        }
        //获取当前时间
        Date now = new Date();
        // 当前时间加半个月（15天）
        Date halfMonthLater = DateUtil.offsetDay(now, 15);

        boolean aftered = vipVO.getEndTime().after(halfMonthLater);
        //只允许提前半个月内续费
        if (aftered){
            return Result.build(null,500,"抱歉，只允许提前半个月内续费");
        }
        //先扣减余额
        guestsService.deductMoney(vipRenewDTO.getPayPwd(),vipVO.getPrice());
        Date newEndTime = DateUtil.offsetMonth(vipVO.getEndTime(), 1);
        vipVO.setEndTime(newEndTime);
        // 续费一个月
        vipMapper.updateById(BeanUtil.copyProperties(vipVO,Vip.class));
        return Result.ok("续费成功");

    }

    @Override
    public List<VipDTO> allVip() {
        return vipMapper.allVip();
    }


}




